<?php

namespace app\common\lib;

use app\common\model\DataContentChip as DataContentChipModel;
use think\facade\Db;

class MakeChip
{
    //条件 优势 流程 支持
    public function run($position)
    {
        if( empty($position) ){
            return null;
        }
        
        $data = [];
        $model = new DataContentChipModel();
        if($position==1 || $position==2){
            $res = $model->getDataByPosition($position, 8); 
            $exist = false;
            foreach($res as $v){
                if(strpos($v['title'],'首先') !== false){
                    array_unshift($data,$v['title']);
                }elseif(strpos($v['title'],'岁') !== false){
                    if(!$exist){
                        array_unshift($data,$v['title']);
                        $exist = true; 
                    }
                }else{
                    array_push($data,$v['title']);
                }
            }
        }elseif($position==3){
            $temp = Db::name('data_content_chip_template')->where('position',$position)->orderRand()->find();
            $arr = explode(',',$temp['value']);
            foreach($arr as $v){
                $res = $model->getDataByPositionOne($position, $v);
                if($res != null){
                    $data[] = $res->title;
                }
            }
        }elseif($position==4){
            $temp = Db::name('data_content_chip_template')->where('position',$position)->orderRand()->find();
            if(!empty($temp['step'])){
                $res = $model->getDataByPosition($position, $temp['step']); 
                foreach($res as $v){
                    $data[] = $v['title'];
                }
            }
        }else{
            return null;
        }
        return $data;
    }

    //获取 条件 优势 流程
    public function getChip($position){
        $str = '';
        if($position == 3){
            $tag = 'ol';
        }else{
            $tag = 'ul';
        }
        $data = $this->run($position);
        if(!empty($data)){
            $num = 0;
            $count = count($data);
            $str .= '<'.$tag.'>';
            foreach ($data as $v) {
                $num++;
                if($num==$count){
                    $v .= '。';
                }else{
                    $v .= '；';
                }
                $str .= '<li>'.$v.'</li>';
            }
            $str .= '</'.$tag.'>';
        }
        return $str;
    }   

    public function replaceContent($str,$arr = ['xiangmu'=>'','dalei'=>'','xiaolei'=>'','wangzhan'=>'']){
        $str = str_replace('www', $arr['wangzhan'], $str);
        $str = str_replace('xxx', $arr['xiangmu'], $str);
        $str = str_replace('dalei', $arr['dalei'], $str);
        $str = str_replace('xiaolei',$arr['xiaolei'], $str);
        return $str;
    }
    
}